 <?php	
$act = (isset($_REQUEST['act'])) ? addslashes($_REQUEST['act']) : "";
@$ip=$_REQUEST['id'];
//unset($_SESSION['info']);

switch($act){

	case "add":
		add();
		$template = "server/add";
		break;
	case "list":
		list_server();
		$template = "server/list";
		break;
	case "delete":
		delete();
		$template = "server/delete";
		break;
	case "update":
		update();
		$template = "server/add";
		break;
	case "info":
		info();
		$template = "server/info";
		break;
	case "start":
		start();
		$template = "server";
		break;
	case "stop":
		stop();
		$template = "server";
		break;
	case "about":
		about_();
		$template = "server/about";
		break;
	case "restart":
		restart();
		$template = "server";
		break;
	case "config":
		config();
		$template = "save";
		break;
	case "save":
		save();
		$template = "server";
		break;
	default:
		$template = "server";
}

function info(){
	global $vidu,$array_server;
	$array_server = array(
	"10.0.0.1",
	"10.0.0.2",
	"10.0.0.3"
	);
	
	$vidu="Hello";

}

function list_server(){
		global $dataset,$myItems;
		$myItems = $dataset->get_records_by_group('ql_server');
		echo $myItems;
		}
function add(){
		global $dataset, $messages, $group_name;
		if(isset($_POST['ip']))
		{
			$id_group = $_POST['id_group'];
			$ip = $_POST['ip'];
			$date = $_POST['date'];
			$netmask = $_POST['netmask'];
			$os = $_POST['os'];
			$dns = $_POST['dns'];
			$gateway = $_POST['gateway'];
			$mac = $_POST['mac'];			
			$inserts = array('id_group' => $id_group, 'ip' => $ip, 'date' => $date, 'netmask' => $netmask, 'gateway' => $gateway, 'os' => $os, 'dns' => $dns, 'mac' => $mac); 
//ham insert trong sql.php
			if($dataset->insert('ql_server', $inserts))
			{
			
				$messages = '	<div class="alert_success">
								<p>
								<img src="images/icon_accept.png" alt="success" class="mid_align"/>
								Success add service.
								</p>
								</div> ';
			}
			else
			{
			
				$messages = '	<div class="alert_error">
							<p>
								<img src="images/icon_error.png" alt="delete" class="mid_align"/>
								Error Notification.
							</p>
						</div>';
			}
			
		}
		$group_name = $dataset->get_records_by_group('ql_group');
		
}

function update(){
global $dataset, $messages, $myItem ;
		
		$id=$_REQUEST['id'];
		//echo $id;
		//die("dung");
		if(isset($_POST['ip']))
		{
			$id_group = $_POST['id_group'];
			$ip = $_POST['ip'];
			$date = $_POST['date'];
			$netmask = $_POST['netmask'];
			$os = $_POST['os'];
			$dns = $_POST['dns'];
			$gateway = $_POST['gateway'];
			$mac = $_POST['mac'];			
			$inserts = array('id_group' => $id_group, 'ip' => $ip, 'date' => $date, 'netmask' => $netmask, 'gateway' => $gateway, 'os' => $os, 'dns' => $dns, 'mac' => $mac); 

			if($dataset->update('ql_server','id',$id, $update))
			{
			
				$messages = '	<div class="alert_success">
								<p>
								<img src="images/icon_accept.png" alt="success" class="mid_align"/>
								Success add group server.
								</p>
								</div> ';
			}
			else
			{
			
				$messages = '	<div class="alert_error">
							<p>
								<img src="images/icon_error.png" alt="delete" class="mid_align"/>
								Error Notification.
							</p>
						</div>';
			}
			
		}
		$myItem = $dataset->get_record_by_ID('ql_server','id', $id);
		//$group_name = $dataset->get_records_by_group('ql_service');
}
function delete(){
global $dataset, $messages, $myItem ;
$id=$_REQUEST['id'];
if(isset($_GET['id']))
		{
if($dataset->delete('ql_server','id',$id))
			{
			
				$messages = '	<div class="alert_success">
								<p>
								<img src="images/icon_accept.png" alt="success" class="mid_align"/>
								Success delete server.
								</p>
								</div> ';
			}
			else
			{
			
				$messages = '	<div class="alert_error">
							<p>
								<img src="images/icon_error.png" alt="delete" class="mid_align"/>
								Error Notification.
							</p>
						</div>';
			}
			}
			
			else
		{
			if(isset($_POST['id']) && count($_POST['id'])!=0)
			{
				$id = $_POST['id'];
				
				foreach($id as $item)
					$dataset->delete('ql_server','id',$item);
					
				transfer("Delete Item ID ".implode(',',$id), "index.php?com=server&act=list");
			}
			
		}
			
		}
function start(){
unset($_SESSION['info']);
global $config,$ip;
$start="start";
//die("dung");
	 $fp = fsockopen($ip, $config['port'], $errno, $errstr, 1);
	 			if(!$fp)
									{
									$_SESSION['info']="Not connect ";
									transfer("Not connect".$ip, "index.php?com=server&act=info");
									}
									else
									{
									// $_SESSION['info']="Connect";
								     fwrite($fp,$start);
									 while (!feof($fp)) 
									{
									 $_SESSION['info'].= fgets($fp, 128)."<br>";
									}
									transfer("Sending data to ".$ip, "index.php?com=server&act=info");	 
									}
}

function about_()
{
global $vidu;
	$vidu="Hello1";
}
function stop(){
unset($_SESSION['info']);
global $config,$ip;
$start="stop";
//die("dung");
	 $fp = fsockopen($ip, $config['port'], $errno, $errstr, 1);
	 			if(!$fp)
									{
									$_SESSION['info']="Not connect ";
									transfer("Not connect".$ip, "index.php?com=server&act=info");
									}
									else
									{
									// $_SESSION['info']="Connect";
								     fwrite($fp,$start);
									 while (!feof($fp)) 
									{
									 $_SESSION['info'].= fgets($fp, 128)."-".$ip."<br>";
									}
									transfer("Sending data to ".$ip, "index.php?com=server&act=info");	 
									}
}
function restart(){
unset($_SESSION['info']);
global $config,$ip;
$start="restart";
//die("dung");
	 $fp = fsockopen($ip, $config['port'], $errno, $errstr, 1);
	 			if(!$fp)
									{
									$_SESSION['info']="Not connect ";
									transfer("Not connect".$ip, "index.php?com=server&act=info");
									}
									else
									{
									// $_SESSION['info']="Connect";
								     fwrite($fp,$start);
									 while (!feof($fp)) 
									{
									 $_SESSION['info'].= fgets($fp, 128)."-".$ip."<br>";
									}
									transfer("Sending data to ".$ip, "index.php?com=server&act=info");	 
									}
}
function config(){
unset($_SESSION['info']);
unset($_SESSION['data']);
global $config,$ip;
$_SESSION['ip']=$ip;
$start="view-config";
//die("dung");
	 $fp = fsockopen($ip, $config['port'], $errno, $errstr, 1);
	 			if(!$fp)
									{
									$_SESSION['info']="Not connect ";
									transfer("Not connect".$ip, "index.php?com=server&act=info");
									}
									else
									{
									$_SESSION['info']="View config - ".$ip;
								     fwrite($fp,$start);
									 while (!feof($fp)) 
									{
									 $_SESSION['data'].= fgets($fp, 128);
									}
									transfer("Sending data to ".$ip, "index.php?com=server&act=info");	 
									}
}
function save(){
unset($_SESSION['info']);
unset($_SESSION['data']);
global $config,$ip;
$start[]=$_POST['config'];
echo $start[1];
die("dung");
	 $fp = fsockopen($_SESSION['ip'], $config['port'], $errno, $errstr, 30);
	 			if(!$fp)
									{
									$_SESSION['info']="Not connect ";
									transfer("Not connect".$ip, "index.php?com=server&act=info");
									}
									else
									{
									$_SESSION['info']="Save config - ".$ip;
									while (true) 
									{
									 fwrite($fp,$start);
									}
								     
									transfer("Sending data to ".$ip, "index.php?com=server&act=info");	 
									}
}
?>